# -*- coding: utf8 -*-

import os
import jinja2
import webapp2

from statistics.word_statistic import *
from environment import *


class ShowWordStatistic(webapp2.RequestHandler):
    def get(self):
        word_text = self.request.get('word')
        word = WordStatistic.get_by_word(word_text)
        html_params = {
            'word_text': word_text,
            'word_exists': False
        }
        if word is not None:
            html_params['word_exists'] = True
            html_params['total_guess'] = 0
            html_params['total_fail'] = 0
            histogram_ttime = []
            histogram_nattempt = []
            histogram_ttime_head = u'[\'Время\', \'Отгадано\', \'Фолов\']'
            histogram_nattempt_head = u'[\'Количество попыток\',\
                \'Отгадано\', \'Фолов\']'
            total_time_sec = 0
            total_games = 0
            for stat in word.hist_ttime:
                html_params['total_guess'] += stat.cnt_guess
                html_params['total_fail'] += stat.cnt_fail
                total_time_sec += stat.time_sec * (stat.cnt_guess +
                                                   stat.cnt_fail)
                total_games += stat.cnt_guess + stat.cnt_fail
                if stat.time_sec < 120:
                    found = False
                    for time_sec, cnt_guess, cnt_fail in histogram_ttime:
                        if time_sec == stat.time_sec:
                            cnt_guess += stat.cnt_guess
                            cnt_fail += stat.cnt_fail
                            found = True
                    if not found:
                        histogram_ttime.append([stat.time_sec, stat.cnt_guess,
                                                stat.cnt_fail])
            for stat in word.hist_ltime_nattempt:
                if stat.num_attempts <= 10:
                    found = False
                    for num_attempts, cnt_guess, cnt_fail in\
                            histogram_nattempt:
                        if num_attempts == stat.num_attempts:
                            cnt_guess += stat.cnt_guess
                            cnt_fail += stat.cnt_fail
                            found = True
                    if not found:
                        histogram_nattempt.append([stat.num_attempts,
                                                   stat.cnt_guess,
                                                   stat.cnt_fail])
            html_params['time_average_sec'] = (float(total_time_sec) /
                                               total_games)
            html_params['guess_percent'] = (html_params['total_guess'] *
                                            100.0 / total_games)
            html_params['fail_percent'] = (html_params['total_fail'] *
                                           100.0 / total_games)
            html_params['total_games'] = total_games
            html_params['histogram_ttime'] = '[' + histogram_ttime_head + ', '
            for elem in histogram_ttime:
                html_params['histogram_ttime'] += str(elem) + ', '
            html_params['histogram_ttime'] += ']'
            html_params['histogram_nattempt'] = ('[' +
                                                 histogram_nattempt_head +
                                                 ', ')
            for elem in histogram_nattempt:
                html_params['histogram_nattempt'] += str(elem) + ', '
            html_params['histogram_nattempt'] += ']'
        template = ENVIRONMENT.get_template('word_statistic.html')
        self.response.write(template.render(html_params))
